package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;

@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
public class CourierAction extends ActionSupport implements ModelDriven<Courier>{
	
	private Courier courier=new Courier();

	@Override
	public Courier getModel() {
		return courier;
	}
	
	@Autowired
	private CourierService courierService;
	
	//添加或修改快递员
	@Action(value="courier_save",results={@Result(name="success",type="redirect",location="./pages/base/courier.html"),
			@Result(name="input",location="./pages/base/error.html")})
	public String save(){
		courierService.save(courier);
		return SUCCESS;
	}
	
	//属性驱动
	private int page;
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	
	//获取ids字符串
	private String ids;
	public void setIds(String ids){
		this.ids=ids;
	}
	
	//分页查询
	@Action(value="courier_pageQuery",results={@Result(type="json")})
	public String pageQuery(){
//		System.out.println("执行到我了"+"==================================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		//创建Specification条件查询对象
		Specification<Courier> specification=new Specification<Courier>(){
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//创建list集合盛放条件对象
				List<Predicate> list=new ArrayList<Predicate>();
				//判断快递员工号,等值查询
				if(StringUtils.isNotBlank(courier.getCourierNum())){
					Predicate p1=cb.equal(root.get("courierNum").as(String.class),courier.getCourierNum());
					list.add(p1);
				}
				//查询公司,模糊查询
				if(StringUtils.isNotBlank(courier.getCompany())){
					Predicate p2=cb.like(root.get("company").as(String.class),"%"+courier.getCompany()+"%");
					list.add(p2);
				}
				//快递员类型查询,等值
				if(StringUtils.isNotBlank(courier.getType())){
					Predicate p3=cb.equal(root.get("type").as(String.class),courier.getType());
					list.add(p3);
				}
				
				//收派标准查询,模糊查询
				//多表查询(查询当前对象所关联对象 对应数据表)
				//Courier(root)关联Standard
				Join<Object,Object> standardRoot=root.join("standard",JoinType.INNER);
				if(courier.getStandard()!=null && StringUtils.isNotBlank(courier.getStandard().getName())){
					Predicate p4=cb.like(standardRoot.get("name").as(String.class),"%"+courier.getStandard().getName()+"%");
					list.add(p4);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
			
		};
		
		Pageable pageable=new PageRequest(page-1,rows); 
		Page<Courier> pageData=courierService.findPageData(specification,pageable);
		
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("total", pageData.getTotalElements());
		map.put("rows", pageData.getContent());
		
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	
	//一键作废快递员
	@Action(value="courier_delBatch",results={@Result(type="redirect",location="./pages/base/courier.html")})
	public String delBatch(){
		
//		System.out.println(ids+"================================================>>>>>>>>>>>>>>>>>>");
		
		String[] idArray=ids.split(",");
		courierService.delBatch(idArray);
		return SUCCESS;
	}
	
	//一键还原快递员
	@Action(value="courier_doRestore",results={@Result(type="redirect",location="./pages/base/courier.html")})
	public String doRestore(){
		String[] idArray=ids.split(",");
		courierService.doRestore(idArray);
		return SUCCESS;
	}
	
	private String fixedAreaId;
	public void setFixedAreaId(String fixedAreaId) {
		this.fixedAreaId = fixedAreaId;
	}
	
	//查询未关联快递员
	@Action(value="courier_findnoassociation",results={@Result(type="json")})
	public String findNoAssociationCourier(){
		List<Courier> couriers=courierService.findByFixedAreaIsNull();
		System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<List<Courier>="+couriers);
		ActionContext.getContext().getValueStack().push(couriers);
		return SUCCESS;
	}
	
}
